package com.situ.cloud.user.controller;


import com.situ.cloud.common.vo.RS;
import com.situ.cloud.user.dto.DeleteRoleDTO;
import com.situ.cloud.user.entity.Role;
import com.situ.cloud.user.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kkkkkk
 * @since 2023-10-24
 */
@Api(description = "角色接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    //根据ID查询角色对象
    @GetMapping("/{id}")
    public RS info(@PathVariable Integer id) {
        return RS.ok(roleService.getById(id));
    }


    //查询角色列表
    @GetMapping("/list")
    public RS list(String keyword, Integer currentPage, Integer pageSize) {
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        return roleService.selectRoleList(keyword, currentPage, pageSize);
    }

    @ApiOperation("删除角色")
    @PostMapping("/delete")
    public RS delete(@RequestBody DeleteRoleDTO dto) {
        roleService.removeBatchByIds(Arrays.asList(dto.getRid().split(",")));
        return RS.ok();
    }

    @PostMapping("/save")
    public RS save(@RequestBody Role role) {
        roleService.doSave(role);
        return RS.ok();
    }

    @ApiOperation("根据ID查询角色对象")
    @GetMapping("/info")
    @ApiImplicitParam(value = "角色ID", name = "rid", required = true,paramType = "query")
    public RS info2(Integer rid) {
        return RS.ok(roleService.getById(rid));
    }
}
